﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace msBsc.WebUi.mMall.Seller
{
    public partial class mfClearBalanceInfo : msBsc.Cls.mBPDealer
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

            }
        }

        private void CustIsSetInnerPayPwd()
        {
            using (msBsc.AppLogic.DataAlc.CBizCustomerRegister objCBizCustomerRegister = new AppLogic.DataAlc.CBizCustomerRegister())
            {
                System.Data.DataSet ds = objCBizCustomerRegister.DataObj.QueryBySQL(@"SELECT CUSTOMER_PAY_INNERPWD FROM DJK_CUSTOMER_REGISTER WHERE CUSTOMER_REGISTER_ID='" + msBsc.SvRef.UserInfo.Operator + "'");

                if (ds == null
                    || ds.Tables[0].Rows.Count <= 0)
                {
                    this.ExecJScript(@"alert(""当前无账号登陆，请登陆"");window.location.href='/mp_UserLogin.htm'");
                    return;
                }

                if (ds.Tables[0].Rows[0]["CUSTOMER_PAY_INNERPWD"] == DBNull.Value
                    || string.IsNullOrEmpty(ds.Tables[0].Rows[0]["CUSTOMER_PAY_INNERPWD"].ToString()))
                {
                    this.ExecJScript(@"alert(""您尚未设置支付密码。请设置支付密码后，再进行支付账号绑定。"");window.location.href=""/ms_SetInnerPayPwd.htm?ReturnUrl=/ms_ClearBalanceInfo.htm""");
                    return;
                }

                ConfirmInnerParPwd();
            }
        }

        private void ConfirmInnerParPwd()
        {
            this.ExecJScript(@"$(""input,select,button"").attr(""disabled"",""disabled"");$(""#pwdInnderPwd"").removeAttr(""disabled"")");
            if (!msBsc.SvRef.UserInfo.UsrIsCmfInnerPwd)
            {
                this.ExecJScript(@"window.location.href=""ms_CmfInnerPayPwd.htm?ReturnUrl=/ms_ClearBalanceInfo.htm""");
                return;
            }

            this.ExecJScript(@"$(""input,select,button"").removeAttr(""disabled"",""disabled"")");
            InitCustBalanceInfo();
        }

        private void InitCustBalanceInfo()
        {
            System.Data.DataSet ds;
            //            DataSet ds = new msBsc.AppLogic.DataAlc.CBizCustomerBaseInfo().DataObj.QueryBySQL(@"SELECT * FROM BSE_BANK ORDER BY SEQ_NUM");

            //            var countryJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 200, "");
            //            this.ExecJScript(@"var _html = $('#bank_name_list').html();
            //                            laytpl(_html).render(" + countryJson + @", function (render) {
            //                                $('#selBankList').html(render);
            //                            });");

            ds = new msBsc.AppLogic.DataAlc.CBizCustomerBaseInfo().DataObj.QueryBySQL(@"SELECT * FROM DJK_CUSTOMER_BASEINFO WHERE CUSTOMER_BASEINFO_ID='" + msBsc.SvRef.UserInfo.Operator + "'");

            if (ds == null || ds.Tables[0].Rows.Count <= 0)
            {
                return;
            }

            //if (ds.Tables[0].Rows[0]["CUSTOMER_BANK_TYPE"] != DBNull.Value
            //    && ds.Tables[0].Rows[0]["CUSTOMER_BANK_TYPE"].ToString() != "")
            //{
            //    this.ExecJScript(@"InitBankInfo('" + ds.Tables[0].Rows[0]["CUSTOMER_BANK_TYPE"].ToString() + "','" + ds.Tables[0].Rows[0]["CUSTOMER_ACCOUNT_NAME"].ToString() + "','" + ds.Tables[0].Rows[0]["CUSTOMER_BANK_ACCOUNT"].ToString() + "','" + ds.Tables[0].Rows[0]["CUSTOMER_BANK_NAME"].ToString() + "')");
            //}

            if (ds.Tables[0].Rows[0]["CUSTOMER_ALIPAY_ACCOUNT"] != DBNull.Value
                && !string.IsNullOrEmpty(ds.Tables[0].Rows[0]["CUSTOMER_ALIPAY_ACCOUNT"].ToString()))
            {
                this.ExecJScript(@"InitAliPayInfo('" + ds.Tables[0].Rows[0]["CUSTOMER_ALIPAY_ACCOUNT"].ToString() + "')");
            }
            else
            {
                this.ExecJScript(@"alert(""您尚未绑定支付宝账户,不能进行解绑!"");window.location.href=""/ms_UserInfo.htm""");
            }

        }

        public override void DoAction()
        {
            if (this.strAction == "CleaAlipay")
            {
                ClearBindAccount("Alipay");
            }

            if (this.strAction == "InitInfo")
            {
                CustIsSetInnerPayPwd();
            }
        }

        private void ClearBindAccount(string accType)
        {
            if (accType == "Alipay")
            {
                using (msBsc.AppLogic.DataAlc.CBizCustomerBaseInfo objCBizCustomerBaseInfo = new AppLogic.DataAlc.CBizCustomerBaseInfo())
                {
                    System.Data.DataSet ds = objCBizCustomerBaseInfo.SelectById(msBsc.SvRef.UserInfo.Operator);

                    ds.Tables[0].Rows[0]["CUSTOMER_ALIPAY_ACCOUNT"] = DBNull.Value;
                    ds.Tables[0].Rows[0]["CUSTOMER_CLEAR_ALIPAY_DT"] = objCBizCustomerBaseInfo.dtCurrDateTime;
                    ds.Tables[0].Rows[0]["CUSTOMER_CREFY_STATUS"] = -1;
                    ds.Tables[0].Rows[0]["CUSTOMER_CREFY_FAILD_NUM"] = 0;
                    ds.Tables[0].Rows[0]["CUSTOMER_IS_CREFY"] = 0;
                    ds.Tables[0].Rows[0]["CUSTOMER_CREFY_DT"] = DBNull.Value;
                    ds.Tables[0].Rows[0]["SYS_FOR_CREFY_DT"] = DBNull.Value;
                    ds.Tables[0].Rows[0]["SYS_FOR_CREFY"] = DBNull.Value;

                    objCBizCustomerBaseInfo.Update(ds);

                    this.ExecJScript(@"alert(""支付宝账户解除绑定成功，为保证您的账户安全，您在7天内不能进行再次绑定！感谢您的支持！"");window.location.href=""/ms_UserInfo.htm""");
                }
            }
        }
    }
}